package org.codeminer.parts.server.actions;

/*
 * Copyright (C) 2013 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import org.codeminer.database.DatabaseException;
import org.codeminer.database.DatabaseManager;
import org.codeminer.parts.constants.MessageConstants;
import org.codeminer.parts.database.PlayerConnector;
import org.codeminer.parts.player.DefaultPlayer;
import org.codeminer.parts.player.Player;
import org.codeminer.parts.player.PlayerManager;
import org.codeminer.parts.server.permissions.Permission;

import java.util.EnumSet;
import java.util.List;
import java.util.Set;

public class DeletePlayer implements Action {

    private final String VERSION = "2";

    private ActionContext context;

    private final Set<Permission> permissions = EnumSet.noneOf(Permission.class);

    private final Long playerId;

    public DeletePlayer(ActionContext context, Long playerId) {
        this.context = context;

        this.playerId = playerId;

        this.permissions.add(Permission.MANAGE_PLAYER);
    }

    @Override
    public String getVersion() {
        return this.VERSION;
    }

    @Override
    public Set<Permission> requiredPermissions() {
        return this.permissions;
    }

    @Override
    public Response execute() {
        try {
            DatabaseManager databaseManager = context.getApplicationPart(DatabaseManager.class);
            PlayerConnector connector = databaseManager.getConnector(PlayerConnector.class);
            List<Player> dbPlayers = connector.find(new DefaultPlayer(getPlayerId()));
            if (dbPlayers.isEmpty() || dbPlayers.size() > 1) {
                return createError(MessageConstants.ACTION_DEFAULT_ERROR, "no single player with id [" + getPlayerId() + "] found");
            } else {
                Player player = dbPlayers.get(0);
                context.getApplicationPart(PlayerManager.class).removePlayer(player);

                connector.delete(player);
                return createOK();
            }
        } catch (DatabaseException e) {
            return createError(MessageConstants.ACTION_DEFAULT_ERROR, e.getMessage());
        }
    }

    public Long getPlayerId() {
        return this.playerId;
    }

    private Response createOK() {
        Response response = new Response("OK");

        return response;
    }

    private Response createError(String messageKey, String message) {
        Response response = new Response("Error");
        response.addProperty("message.key", messageKey);
        response.addProperty("message", message);
        return response;
    }

}
